#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "chooselanguage.h"
#include "checkiso.h"
#include "choosezone.h"
#include "createuser.h"
#include "partitiondisk.h"
#include "install.h"
#include "finish.h"
#include "agreementdialog.h"
#include "rebootui.h"

#include <QIcon>
#include <QScreen>
#include <QPainter>
#include <QComboBox>
#include <qglobal.h>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    , m_screenScale(WTH16X9)
    , m_currentPageIndex(0)
{
    ui->setupUi(this);
    setWindowTitle(tr("install wizard"));

    QScreen *screen = QGuiApplication::primaryScreen();
    QRect geometry = screen->geometry();
    int width = geometry.width();
    int height = geometry.height();
    double scale = (double)width/height;
    if (scale == (double)16/10)
        m_screenScale = WTH16X10;
    else if (scale == (double)16/10)
        m_screenScale = WTH4X3;

    qInfo() << "screen scale:" << m_screenScale;
    switch (m_screenScale) {
    case WTH16X10:
        m_bg = ":/icons/bg16x10.png";
        break;
    case WTH16X9:
        m_bg = ":/icons/bg16x9.png";
        break;
    case WTH4X3:
        m_bg = ":/icons/bg4x3.png";
        break;
    default:
        break;
    }

    ui->preview_Btn->setIcon(QIcon(":/icons/preview_step.png"));

    initPages();
    onCurrentPageChanged(0);
    connect(this, &MainWindow::currentPageChanged, this, &MainWindow::onCurrentPageChanged);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::paintEvent(QPaintEvent *pEvent)
{
    QPainter painter(this);
    painter.drawPixmap(0, 0, this->width(), this->height(), QPixmap(m_bg));
}

void MainWindow::hideCentralContent()
{
    m_pages[m_currentPageIndex]->hide();
}

void MainWindow::showCentralContent()
{
    m_pages[m_currentPageIndex]->show();
}

void MainWindow::hideCurrentStepContent()
{
    m_pages[m_currentPageIndex]->hide();
    ui->next_Btn->hide();
}

void MainWindow::showCurrentStepContent()
{
    m_agreementDialog->hide();

    m_pages[m_currentPageIndex]->show();
    ui->next_Btn->show();
}

void MainWindow::showAgreementDialog(int ty, bool state)
{
    hideCurrentStepContent();
    m_agreementDialog->setCurrentContent(ty);
    m_agreementDialog->show();
}

void MainWindow::onAgreeStateChanged(int state)
{
    ui->next_Btn->setEnabled(state);
}

void MainWindow::on_preview_Btn_released()
{
    if (m_currentPageIndex == 0)
        return;

    if (!m_pages[m_currentPageIndex]->previewStep()) {
        return;
    }

    Q_EMIT currentPageChanged(--m_currentPageIndex);
}

void MainWindow::on_next_Btn_released()
{
    if (!m_pages[m_currentPageIndex]->nextStep()) {
        return;
    }

    if (m_currentPageIndex+1 == m_pages.size())
        return;

    Q_EMIT currentPageChanged(++m_currentPageIndex);
}

void MainWindow::onCurrentPageChanged(int index)
{
    if (index < 0 || index >= m_pages.size())
        return;

    ui->currentStep_Label->setPixmap(QString(":/icons/progress%1.png").arg(index+1));
    if (index == 0)
        ui->preview_Btn->hide();
    else
        ui->preview_Btn->show();

    if (index == m_pages.size() - 1) {
        ui->preview_Btn->hide();
        ui->next_Btn->hide();
        ui->exit_Btn->hide();

        showRebootUI();
    } else {
        ui->next_Btn->show();
    }

    if (m_pages[m_currentPageIndex]->getModuleName() == "checkiso") {
        ui->next_Btn->setEnabled(true);
        if (m_checkState == CHECKFAILED) {
            ui->next_Btn->setText(tr("check again"));
        } else {
            ui->next_Btn->setText(tr("Next"));
        }
    }

    changePage();
}

void MainWindow::onUpdateCheckState(CheckState state)
{
    m_checkState = state;

    if (m_checkState == CHECKING) {
        ui->next_Btn->setEnabled(false);
        ui->next_Btn->setText(tr("Next"));
    } else if (m_checkState == CHECKSUCCESSED){
        ui->next_Btn->setText(tr("Next"));
        ui->next_Btn->setEnabled(true);
    } else if  (m_checkState == CHECKFAILED) {
        ui->next_Btn->setText(tr("check again"));
        ui->next_Btn->setEnabled(true);
    }
}

void MainWindow::onContinueReboot()
{
    m_rebootUI->hide();

    ui->next_Btn->setText(tr("Continue Reboot"));
    ui->next_Btn->show();
}

void MainWindow::onRebootCbx(int index)
{
    // last page; TODO show reboot button
    qInfo() << "index: " << index << " op: " << (index == 0 ? "reboot" : "shutdown");


}

void MainWindow::on_exit_Btn_released()
{
    qInfo() << "It's about to quit.";
    QCoreApplication::quit();
}

void MainWindow::initPages()
{
    // page 1
    ChooseLanguage *cl = new ChooseLanguage(ui->central_widget);
    // agreement dialog
    m_agreementDialog = new AgreementDialog(ui->central_widget);
    m_agreementDialog->hide();
    ui->next_Btn->setEnabled(false);
    connect(m_agreementDialog, &AgreementDialog::fakeClose, this, &MainWindow::showCurrentStepContent);
    connect(m_agreementDialog, &AgreementDialog::stateChanged, cl, &ChooseLanguage::onStateChanged);
    connect(cl, &ChooseLanguage::showAgreementDialog, this, &MainWindow::showAgreementDialog);
    connect(cl, &ChooseLanguage::stateChanged, m_agreementDialog, &AgreementDialog::onStateChanged);
    connect(cl, &ChooseLanguage::stateChanged, this, &MainWindow::onAgreeStateChanged);
    cl->setParent(ui->central_widget);


    // page 2
    CheckISO *check = new CheckISO(ui->central_widget);
    connect(check, &CheckISO::updateCheckState, this, &MainWindow::onUpdateCheckState);
    check->setParent(ui->central_widget);

    // page 3
    ChooseZone *zone = new ChooseZone(ui->central_widget);
    zone->setParent(ui->central_widget);

    // page 4
    CreateUser *create = new CreateUser(ui->central_widget);
    create->setParent(ui->central_widget);

    // page 5
    PartitionDisk *partition = new PartitionDisk(ui->central_widget);
    partition->setParent(ui->central_widget);

    // page 6
    Install *install =  new Install(ui->central_widget);
    install->setParent(ui->central_widget);

    // page 7
    Finish *finish =  new Finish(ui->central_widget);
    // reboot UI
    m_rebootUI= new RebootUI(this);
    m_rebootUI->hide();
    connect(m_rebootUI, &RebootUI::continueReboot, this, &MainWindow::onContinueReboot);
    connect(m_rebootUI, &RebootUI::continueReboot, finish, &Finish::onContinueReboot);
    finish->setParent(ui->central_widget);

    m_pages << cl << check << zone << create << partition << install << finish;
}

void MainWindow::changePage()
{
    for (int i=0; i< m_pages.size(); ++i) {
        if (i == m_currentPageIndex) {
            m_pages[i]->show();
            m_pages[i]->start();
        } else {
            m_pages[i]->hide();
        }
    }
}

void MainWindow::showRebootUI()
{
    QPoint parentPos = this->mapToGlobal(QPoint(0, 0));
    QPoint nextBtnPos = ui->next_Btn->mapToGlobal(QPoint(0, 0));
    QPoint centerPoint = (nextBtnPos - parentPos) + QPoint(ui->next_Btn->width() / 2, ui->next_Btn->height() / 2);

    m_rebootUI->resize(300, 200);
    m_rebootUI->move(centerPoint.x() - 150, centerPoint.y() - 100);
    m_rebootUI->show();
}
